package wrs.hcs.functions;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import wrs.hcs.functions.PatientName;

public class AdminConsole {
	
	static
	Miscellaneous m = new Miscellaneous();
	
	//------------------------------------------------------------------------------------------------------------
	//THIS PORTION CORRESPONDS TO PATIENT INFORMATION WEB FORM FOR UPDATES
	
	//WELLNESS SEARCH PIN or LASTNAME
	public void wellness_search_it(WebDriver driver, String pin, String fname, String date){
		
		driver.findElement(By.xpath("//input[@id='plname']")).clear();
		driver.findElement(By.xpath("//input[@id='plname']")).sendKeys(pin);
		if(!fname.isEmpty()||!fname.equals("")){
			try{
				driver.findElement(By.xpath("//input[@id='fname']")).clear();
				driver.findElement(By.xpath("//input[@id='datepicker2']")).clear();
				driver.findElement(By.xpath("//input[@id='datepicker3']")).clear();
			}catch(Exception e){
				driver.findElement(By.xpath("//a[@id='displayText']")).click();
				driver.findElement(By.xpath("//input[@id='fname']")).clear();
				driver.findElement(By.xpath("//input[@id='datepicker2']")).clear();
				driver.findElement(By.xpath("//input[@id='datepicker3']")).clear();
			}
			driver.findElement(By.xpath("//input[@id='fname']")).sendKeys(fname);
			driver.findElement(By.xpath("//input[@id='datepicker2']")).sendKeys(date);
			driver.findElement(By.xpath("//input[@id='datepicker3']")).sendKeys(date);
		}
		WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(
		    ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='SEARCH']")));
		driver.findElement(By.xpath("//input[@value='SEARCH']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//WELLNESS GET STATUS OF RECORD AND CLICK
	public String get_status_and_click(WebDriver driver, String pin, boolean click){
		
		//Search for patient
		List<WebElement> rows = driver.findElements(By.xpath("//table[@id='table']/tbody/tr"));
		int size = rows.size();
		
		System.out.println("Size of rows: "+size);
		
		int size1 = size;
		int counter=0;
		String newPin="";
		

		
		if(pin.isEmpty()||pin.equals("")){
			for(int y=0;y<size1;size1--){
				newPin = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size1+"]/td[4]")).getText();
				System.out.println("Assigned PIN : "+newPin);
				if(counter==0){
					pin=newPin;
					counter++;
				}else{
					pin=pin+","+newPin;
				}
			}
		}
		
		for(int x=0; size>x; size--)
		{
			String pin1="", pin2="";
			if(!pin.equals(",")){
				if(pin.contains(",")){
					String[] pins = pin.split(",");
					pin1 = pins[0];
					pin2 = pins[1];
				}
			}
			
			String tablePin = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[4]")).getText(); // get pin of row
			if(pin.equals(tablePin)||((tablePin.isEmpty()||tablePin.equals("")) && size ==1)||pin1.equals(tablePin)||pin2.equals(tablePin))
			{
				System.out.println(
						"Status of Transaction is: "+
						driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[19]")).getText());
				if(click==true){
					driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[3]/a")).click();	
				}
			}
		}
		
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		return pin;
	}
	//SELECT UNIT OF TRANSACTION
	public void select_unit_single(WebDriver driver, String type){
		if(type.equals("webapp")){
			WebDriverWait wait = new WebDriverWait(driver,30);
			wait.until(
				    ExpectedConditions.elementToBeClickable(
				        By.xpath("//select[@id='packageRooms']")));
		}
		Select nurse = new Select(driver.findElement(By.xpath("//select[@id='nursingUnit']")));
		nurse.selectByVisibleText("10TH FLOOR NW PULMONARY CARE UNIT");
		System.out.println("Unit is selected");
	}
	//SELECT ROOM FOR SINGLE TRANSACTION ONLY
	public void select_room_single(WebDriver driver){
		driver.findElement(By.xpath("//img[@src='/images/icon_search.png']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//table[@id='roomBedTable']/tbody/tr[1]/td[1]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		System.out.println("Room is selected");
	}
	//SELECT ROOM FOR PAIR TRANSACTION ONLY
	public void select_room_pair(WebDriver driver, int x){
		driver.findElement(By.xpath("//a[@id='roomBedPop']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		window_focus_next(driver);
		Actions actions = new Actions(driver);
        if(x==1){
        	WebElement mouseover = driver.findElement(By.xpath("//table[@id='roomBedTable']/tbody/tr[1]"));
        	actions.moveToElement(mouseover);
        }else{
        	WebElement mouseover = driver.findElement(By.xpath("//table[@id='roomBedTable']/tbody/tr[2]"));
        	actions.moveToElement(mouseover);
        }    
        actions.click().perform();
	}
	//SELECT DOCTOR FOR SINGLE TRANSACTION ONLY
	public void select_doctor_single(WebDriver driver, String type, String mainWindow){
		
		driver.findElement(By.xpath("//button[@id='dialog_link']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		if(type.equals("pair")){
			driver.switchTo().window(mainWindow);
		}
		System.out.println(driver.findElement(By.xpath("//a[@id='5825']")).getText());
		driver.findElement(By.xpath("//a[@id='5825']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//SELECT PATIENT CARE OFFICER
	public void select_patient_care_officer(WebDriver driver){
		Select pCareOfficer = new Select(driver.findElement(By.xpath("//select[@name='patientCare']")));
		pCareOfficer.selectByVisibleText("CUEVAS ,CYRIELLE GAYLE GABRIEL");
	}
	//METHOD FOR UPDATING SINGLE TRANSACTION (INPATIENT OR OUTPATIENT)
	public void update_online_single_in_admin(WebDriver driver, String type){
		if(type.equals("inpatient")){
			select_unit_single(driver, "webapp");
			select_room_single(driver);
		}
		select_doctor_single(driver, "single", "");
		select_patient_care_officer(driver);
		submit_form(driver);
		close_popup_after_submit_form(driver);
		for(int z=0; z<17; z++){
			driver.findElement(By.xpath("//input[@class='btnPrimary']")).sendKeys(Keys.ARROW_DOWN);	
		}
		select_back_button(driver);
		
	}
	//SELECT DROPDOWN FOR ACTION TO HCS (FORWARD TO HCS and etc)
	public void select_dropdown_to_hcs(WebDriver driver, String text, String type){
		//NOTE: Search first before calling this method
		//The next line selects the transaction to be integrated to HCS
		driver.findElement(By.xpath("//table[@id='table']/tbody/tr[1]/td[1]/input")).click();
		if(type.equals("pair")){
			driver.findElement(By.xpath("//table[@id='table']/tbody/tr[2]/td[1]/input")).click();
		}	
		Select action = new Select(driver.findElement(By.xpath("//select[@id='action']")));
		action.selectByVisibleText(text);
		driver.findElement(By.xpath("//input[@value='APPLY']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		if(text.contains("CANCEL")||text.contains("REACTIVATE")){
			String[] disect = text.split(" ");
			driver.findElement(By.xpath("//textarea[@id='"+disect[0].toLowerCase().toString()+"Reason"+"']")).sendKeys(disect[0]+" Transaction by Selenium");
			driver.findElement(By.xpath("//input[@id='"+disect[0].toLowerCase().toString()+"Reserve"+"']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}
	}
	//METHOD FOR UPDATING PAIR TRANSACTIONS
	public void update_pair_online_in_admin(WebDriver driver){
		select_unit_single(driver, "webapp");
		select_room_single(driver);
		select_room_pair(driver, 1);
		select_patient_care_officer(driver);
	}
	//METHOD FOR FORWARDING TRANSACTIONS
	public void forward(WebDriver driver, String pin){
		wellness_search_it(driver, pin, "", "");
		get_status_and_click(driver, pin, false);
		String text = m.set_dropdown_to_hcs("online");
		select_dropdown_to_hcs(driver, text, "single");
		display_message_to_hcs(driver);
		wellness_search_it(driver, pin, "", "");
		get_status_and_click(driver, pin, false);
	}
	//GET THE VALIDATION ON TABLE AFTER SAVING
	public void table_validation(WebDriver driver, String package_type, String transaction_type){
		List<WebElement> rows = driver.findElements(By.xpath("//table[@id='table']/tbody/tr"));
		int size = rows.size();
		
		// PATIENT TYPE : INPATIENT or OUTPATIENT
		String table_patient_type = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[9]")).getText();
		table_patient_type = table_patient_type.toLowerCase().toString();
		if(table_patient_type.equals(transaction_type)){
			System.out.println(table_patient_type+" is CORRECT");
		}else{
			System.out.println(table_patient_type+" is not equals to : "+ transaction_type + " .INCORRECT");
		}
		
		// PATIENT PACKAGE 
		String table_patient_package = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[8]")).getText();
		
		if(package_type.equals("online") && transaction_type.equals("inpatient")){
			
			if(table_patient_package.equals("CARDIOMETABOLIC SCREENING < 50 Y/O")){
				System.out.println(table_patient_package+" is CORRECT");
			}else{
				System.out.println(table_patient_package+" is INCORRECT");
			}
		}else if(package_type.equals("nononline") && transaction_type.equals("outpatient")){
			
			if(table_patient_package.equals("AMKOR PACKAGE MALE - OPD")){
				System.out.println(table_patient_package+" is CORRECT");
			}else{
				System.out.println(table_patient_package+" is INCORRECT");
			}
		}else if(package_type.equals("online") && transaction_type.equals("outpatient")){
			
			if(table_patient_package.equals("OVERWEIGHT SCREENING <50Y/O")){
				System.out.println(table_patient_package+" is CORRECT");
			}else{
				System.out.println(table_patient_package+" is INCORRECT");
			}
		}else if(package_type.equals("nononline") && transaction_type.equals("inpatient")){
			
			if(table_patient_package.equals("BSP A - INPATIENT")){
				System.out.println(table_patient_package+" is CORRECT");
			}else{
				System.out.println(table_patient_package+" is INCORRECT");
			}
		}
		else if(package_type.equals("single_online") && (transaction_type.equals("inpatient")||transaction_type.equals("outpatient"))){
			
			if(table_patient_package.contains("BASIC HEALTH SCREENING")){
				System.out.println(table_patient_package+" is CORRECT");
			}else{
				System.out.println(table_patient_package+" is INCORRECT");
			}
		}
		
		// PATIENT ADDON
		String online[] = {
				"BLOOD TYPING",
				"BODY FAT ANALYSIS"
		};
		
		String nononline[] = {
				"BASIC EYE EXAM",
				"BRAIN CT-SCAN"
		};
		String addon1 = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[13]/ul/li[1]")).getText();
		String addon2 = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[13]/ul/li[2]")).getText();
		
		if(package_type.equals("online")||package_type.contains("online")){
			
			if(
			   (addon1.equals(online[0]) || addon1.equals(online[1])) &&
			   (addon2.equals(online[0]) || addon2.equals(online[1]))
			  ){
				System.out.println("AddOns are CORRECT");
			}else{
				System.out.println(
						addon1 +" , "+ addon2 +" , "+"are INCORRECT");
			}

		}else if(package_type.equals("nononline")){
			
			if(
				(addon1.equals(nononline[0]) || addon1.equals(nononline[1])) &&
				(addon2.equals(nononline[0]) || addon2.equals(nononline[1]))
			  ){
				System.out.println("AddOns are CORRECT");
			}else{
				System.out.println(
						addon1 +" , "+ addon2 +" , "+"are INCORRECT");
			}
			
		}
		
	}
	//GET THE VALIDATION ON TABLE FOR PAIR TRANSACTION AFTER SAVING
	public void pair_table_validation(WebDriver driver, String package_type, String transaction_type){
		List<WebElement> rows = driver.findElements(By.xpath("//table[@id='table']/tbody/tr"));
		int size = rows.size();
		
		for(int x=0; x<size; size--){
			
			// PATIENT TYPE : INPATIENT or OUTPATIENT
			String table_patient_type = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[9]")).getText();
			table_patient_type = table_patient_type.toLowerCase().toString();
			if(table_patient_type.equals(transaction_type)){
				System.out.println(table_patient_type+" is CORRECT");
			}else{
				System.out.println(table_patient_type+" is not equals to : "+ transaction_type + " .INCORRECT");
			}
			
			// PATIENT PACKAGE 
			String table_patient_package = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[8]")).getText();
			String package1="", package2="";
			
			if(!package_type.contains("admin")){
				package2 = "2-IN-ROOM OVERWEIGHT SCREENING";
				package1 = "2-IN-ROOM BASIC HEALTH SCREENING";
			}else if(package_type.equals("admin_online")){
				package1 = package2 = "2-IN-ROOM CARDIOMETABOLIC SCREENING";
			}else if(package_type.equals("admin_nononline")){
				package1 = package2 = "PLAN A1 - 2 IN ROOM HW";
			}
			
			if(size==2){
				if(table_patient_package.contains(package2)){
					System.out.println(table_patient_package+" is CORRECT");
				}else{
					System.out.println(table_patient_package+" is INCORRECT");
				}
			}else if(size==1){
				if(table_patient_package.contains(package1)){
					System.out.println(table_patient_package+" is CORRECT");
				}else{
					System.out.println(table_patient_package+" is INCORRECT");
				}
			}
			
			// PATIENT ADDON
			String online[] = {
					"BLOOD TYPING",
					"BODY FAT ANALYSIS"
			};
			
			String nononline[] = {
					"BASIC EYE EXAM",
					"BRAIN CT-SCAN"
			};
			String addon1 = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[13]/ul/li[1]")).getText();
			String addon2 = driver.findElement(By.xpath("//table[@id='table']/tbody/tr["+size+"]/td[13]/ul/li[2]")).getText();
			
			if(package_type.equals("online")||package_type.contains("_online")){
				
				if(
				   (addon1.equals(online[0]) || addon1.equals(online[1])) &&
				   (addon2.equals(online[0]) || addon2.equals(online[1]))
				  ){
					System.out.println("AddOns are CORRECT");
				}else{
					System.out.println(
							addon1 +" , "+ addon2 +" , "+"are INCORRECT");
				}

			}else if(package_type.equals("nononline")||package_type.contains("_nononline")){
				
				if(
					(addon1.equals(nononline[0]) || addon1.equals(nononline[1])) &&
					(addon2.equals(nononline[0]) || addon2.equals(nononline[1]))
				  ){
					System.out.println("AddOns are CORRECT");
				}else{
					System.out.println(
							addon1 +" , "+ addon2 +" , "+"are INCORRECT");
				}
			}
		}
	}
	//CHECK IF PATIENT NAME ENDS WITH SEP, if tagged as VIP
	public void check_patient_name_if_ends_at_sep(WebDriver driver){
		String sep = driver.findElement(By.xpath("//table[@id='table']/tbody/tr/td[3]/span")).getText();
		if(sep.equals("(SEP)")){
			System.out.println("Name Ends with SEP. CORRECT");
		}else{
			System.out.println("Name does not End with SEP. INCORRECT");
		}
	}
	
	//------------------------------------------------------------------------------------------------------------
	//THIS PORTION CORRESPONDS TO PATIENT INFORMATION WEB FORM FOR CREATING NEW TRANSACTIONS
	
	//FILL UP RESERVATION TYPE OF PATIENT
	public void input_the_reservation_type_of_patient(WebDriver driver, String option){
		WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(
		    ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='lastName']")));
		Select reserve = new Select(driver.findElement(By.xpath("//select[@id='reservationType']")));
		if(option.equals("email")){
			reserve.selectByVisibleText("EMAIL");
		}else if(option.equals("phone")){
			reserve.selectByVisibleText("PHONE CALL");
		}else if(option.equals("walkin")){
			reserve.selectByVisibleText("WALK-IN");
		}else{
			System.out.println("Incorrect option set in PATIENT INFORMATION RESERVATION TYPE");
		}
	}
	//FILLUP NAME(S) OF PATIENT
	public String input_the_names_of_patient(WebDriver driver) throws Exception{
		WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(
		    ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='lastName']")));
		WebElement lName = driver.findElement(By.xpath("//input[@id='lastName']"));
		WebElement fName = driver.findElement(By.xpath("//input[@id='firstName']"));
		WebElement mName = driver.findElement(By.xpath("//input[@id='middle_name']"));
		lName.click();
		
		//PATIENT NAME
		PatientName pName = new PatientName();
		String lastName = pName.names(1);
		wait.until(
			    ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='lastName']")));
		lName.sendKeys(lastName);
		lName.clear();
		lName.sendKeys(lastName);
		String firstName = pName.names(2);
		fName.sendKeys(firstName);
		String middleName = pName.names(3);
		mName.sendKeys(middleName);
		Select suffix = new Select(driver.findElement(By.xpath("//select[@id='suffixCode']")));
		suffix.selectByVisibleText("JR");
		String finalName = lastName + ","+firstName;
		
		System.out.println("Name of Patient: " + lastName + ", "+firstName+  " JR " + middleName);
		return finalName;
	}
	//FILLUP BIRTHDAY OF PATIENT
	public void input_the_birthday_of_patient(WebDriver driver){
		driver.findElement(By.xpath("//input[@id='dateOfBirth']")).sendKeys("01/01/1997");
	}
	//FILLUP GENDER OF PATIENT
	public void input_the_gender_of_patient(WebDriver driver, String option){
		Select gender = new Select(driver.findElement(By.xpath("//select[@id='genderCode']")));
		if(option.equals("male")){
			gender.selectByVisibleText("MALE");
		}else{
			gender.selectByVisibleText("FEMALE");
		}
	}
	//FILLUP CONTACT NUMBER OF PATIENT
	public void input_the_contact_number_of_patient(WebDriver driver){
		driver.findElement(By.xpath("//input[@id='areaCode']")).clear();
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@id='areaCode']")).clear();
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@id='areaCode']")).clear();
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@id='areaCode']")).clear();
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@id='areaCode']")).clear();
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@id='areaCode']")).clear();
		driver.findElement(By.xpath("//input[@id='areaCode']")).sendKeys("44");
		driver.findElement(By.xpath("//input[@id='phoneNum']")).clear();
		driver.findElement(By.xpath("//input[@id='phoneNum']")).sendKeys("9359383886");
	}
	//FILLUP EMAIL ADDRESS OF PATIENT
	public void input_the_email_address_of_patient(WebDriver driver){
		Actions actions = new Actions(driver);
        WebElement mouseover = driver.findElement(By.xpath("//input[@id='emailAdd']"));
        actions.moveToElement(mouseover);
        actions.doubleClick().perform();
        actions.sendKeys(mouseover, "jude_bordallo@yahoo.com");
	}
	//FILLUP PWD NUMBER OF PATIENT
	public void input_the_pwd_of_patient(WebDriver driver){
		driver.findElement(By.xpath("//input[@name='pwd_id']")).sendKeys("1395");
	}
	//FILLUP SENIOR ID OF PATIENT
	public void input_the_senior_id_of_patient(WebDriver driver){
		driver.findElement(By.xpath("//input[@name='senior_id']")).sendKeys("5462");
	}
	//FILLUP PACKAGE OF PATIENT
	public void input_the_package_of_patient(WebDriver driver, String option){
		Select pack = new Select(driver.findElement(By.xpath("//select[@id='packageRateNo']")));
		if(option.equals("single")){
			pack.selectByVisibleText("BSP F package (INPATIENT) - 50 Y/O and ABOVE");
		}else if(option.equals("outpatient")){
			pack.selectByVisibleText("AMKOR PACKAGE (OUTPATIENT)");
		}else if(option.equals("onlinebasic")){
			pack.selectByVisibleText("BASIC HEALTH SCREENING PACKAGE");
		}else if(option.equals("onlinecardio")){
			pack.selectByVisibleText("CARDIOMETABOLIC SCREENING PACKAGE");
		}else if(option.equals("onlinediabetes")){
			pack.selectByVisibleText("DIABETES SCREENING PACKAGE");
		}else if(option.equals("onlinegolden")){
			pack.selectByVisibleText("GOLDEN HEALTH SCREENING PACKAGE");
		}else if(option.equals("onlineoverweight")){
			pack.selectByVisibleText("OVERWEIGHT SCREENING PACKAGE");
		}else if(option.equals("outnononline")){
			pack.selectByVisibleText("BSP A package (INPATIENT) - 40 Y/O BELOW");
		}else if(option.equals("pairnononline")){
			pack.selectByVisibleText("ANNUAL physical check-UP package A1");
		}
	}
	//FILLUP DATE OF ADMISSION OF PATIENT
	public String input_the_date_of_admission_of_patient(WebDriver driver, String type){
		String date;
		if(type.equals("current")){
			date = m.set_current_date_plus_one();	
		}else{
			date = m.set_date_webapp();	
		}
		System.out.println(date);
		driver.findElement(By.xpath("//input[@id='datepicker2']")).sendKeys(date);
		return date;
	}
	//FILLUP TIME OF ADMISSION OF PATIENT
	public void input_the_time_of_admission_of_patient(WebDriver driver){
		Select time = new Select(driver.findElement(By.xpath("//select[@id='arrivalTime']")));
		try{
			time.selectByVisibleText("12:00 PM");	
		}catch(Exception e){
			time.selectByVisibleText("12:30 PM");
		}
	}
	//FILLUP TIME OF ROOM TYPE OF PATIENT
	public void input_the_room_type_of_patient(WebDriver driver, String option){
		Select room = new Select(driver.findElement(By.xpath("//select[@id='packageRooms']")));
		if(option.equals("private")){
			room.selectByVisibleText("PRIVATE");
		}else if(option.equals("suite")){
			room.selectByVisibleText("SUITE");
		}else if(option.equals("opd")){
			room.selectByVisibleText("OPD");
		}else if(option.equals("presidential")){
			room.selectByVisibleText("PRESIDENTIAL");
		}
	}
	//FILLUP TIME OF BOOKER OF PATIENT
	public void input_the_booker_of_patient(WebDriver driver){
		//THIS BOOKER CODE IS ALWAYS SET TO SELF
		Select booker = new Select(driver.findElement(By.xpath("//select[@id='bookerRelationTo']")));
		booker.selectByVisibleText("SELF");
	}
	//FILLUP PERSON/PARTY RESPONSIBLE FOR THIS ACCOUNT OF PATIENT
	public void input_the_responsible_for_patient(WebDriver driver, String option){
		
		if(option.equals("self")){
			driver.findElement(By.xpath("//input[@id='IND2']")).click();
			Select select2 = new Select(driver.findElement(By.xpath("//select[@id='select2']")));
			select2.selectByVisibleText("SELF");
		}else if(option.equals("hmo")){
			driver.findElement(By.xpath("//input[@id='HMO2']")).click();
			Select select2 = new Select(driver.findElement(By.xpath("//select[@id='selectHMO']")));
			select2.selectByVisibleText("COCOLIFE HEALTH CARE");
		}else if(option.equals("company")){
			driver.findElement(By.xpath("//input[@id='COM2']")).click();
			Select select2 = new Select(driver.findElement(By.xpath("//select[@id='selectCOM']")));
			select2.selectByVisibleText("BANGKO SENTRAL NG PILIPINAS");
		}else if(option.equals("others")){
			driver.findElement(By.xpath("//input[@id='OTH']")).click();
			driver.findElement(By.xpath("//input[@id='others']")).sendKeys("OTHERS GOES HERE");
		}
		driver.findElement(By.xpath("//input[@name='loa_flag']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//METHOD FOR CREATING SINGLE INPATIENT WITH NO PIN IN ADMIN
	public String create_single_inpatient_with_no_pin_in_admin(WebDriver driver, String type, boolean header) throws Exception{
		add_reservation(driver, "single");
		new_patient_button(driver);
		m.refresh_window(driver);
		input_the_reservation_type_of_patient(driver, "phone");
		String name = input_the_names_of_patient(driver);
		if(header==true){
			header_patient_information(driver, true, true, true);
		}
		input_the_birthday_of_patient(driver);
		input_the_gender_of_patient(driver, "male");
		input_the_contact_number_of_patient(driver);
		input_the_package_of_patient(driver, type);
		String date = input_the_date_of_admission_of_patient(driver, "");
		input_the_time_of_admission_of_patient(driver);
		if(!type.equals("outpatient")){
			select_unit_single(driver, "admin");
			input_the_room_type_of_patient(driver, "private");
		}
		select_doctor_single(driver, "single", "");
		input_the_booker_of_patient(driver);
		select_patient_care_officer(driver);
		input_the_responsible_for_patient(driver, "self");
		submit_form(driver);
		close_popup_after_save_new_patient(driver);
		String returned_string = name+","+date;
		return returned_string;
	}
	//SEARCH PIN ON WRS ADMIN CONSOLE
	public void search_pin_on_admin_console(WebDriver driver, String pin1, String pin2, String type){
		WebElement pin = driver.findElement(By.xpath("//input[@name='pinLastName']"));
		
		if(type.equals("single")){
			pin.sendKeys(pin1);
			driver.findElement(By.xpath("//input[@value='SUBMIT']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//table[@id='table']/tbody/tr/td[7]/a/strong")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}else{
			pin.sendKeys(pin1);
			driver.findElement(By.xpath("//input[@value='SUBMIT']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//table[@id='table']/tbody/tr/td[7]/input")).click();
			pin = driver.findElement(By.xpath("//input[@name='pinLastName']"));
			pin.sendKeys(pin2);
			driver.findElement(By.xpath("//input[@value='SUBMIT']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//table[@id='table']/tbody/tr/td[7]/input")).click();
			driver.findElement(By.xpath("//input[@id='btnReserve']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}

	}
	//METHOD FOR CREATING SINGLE INPATIENT WITH PIN IN ADMIN
	public void create_single_inpatient_with_pin_in_admin(WebDriver driver, String pin, String room) throws Exception{
		add_reservation(driver, "single");
		search_pin_on_admin_console(driver, pin, "", "single");
		m.refresh_window(driver);
		input_the_reservation_type_of_patient(driver, "phone");
		input_the_contact_number_of_patient(driver);
		input_the_package_of_patient(driver, "onlineoverweight");
		input_the_date_of_admission_of_patient(driver,"");
		input_the_time_of_admission_of_patient(driver);
		select_unit_single(driver, "admin");
		input_the_room_type_of_patient(driver, room);
		select_doctor_single(driver, "single", "");
		input_the_booker_of_patient(driver);
		select_patient_care_officer(driver);
		input_the_responsible_for_patient(driver, "self");
		submit_form(driver);
		close_popup_after_save_new_patient(driver);
	}
	//METHOD FOR CREATING ADDON IN WEBADIN
	public void create_addon(WebDriver driver, String package_type, String type){
		driver.findElement(By.xpath("//button[@id='createAddOns']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		Select addOn = new Select(driver.findElement(By.xpath("//select[@id='addOnsList']")));
		if(type.equals("pair")){
			JavascriptExecutor jse = (JavascriptExecutor)driver;
			jse.executeScript("window.scrollBy(0,250)", "");
		}
		if(package_type.equals("online")){
			
			addOn.selectByVisibleText("BLOOD TYPING");
			driver.findElement(By.xpath("//input[@value='>']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			addOn.selectByVisibleText("BODY FAT ANALYSIS");
			driver.findElement(By.xpath("//input[@value='>']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			
		}else if(package_type.contains("nononline")){
			
			addOn.selectByVisibleText("BRAIN CT-SCAN");
			driver.findElement(By.xpath("//input[@value='>']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			addOn.selectByVisibleText("BASIC EYE EXAM");
			driver.findElement(By.xpath("//input[@value='>']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			
		}
			
		driver.findElement(By.xpath("//button[@id='okAddOn']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	//METHOD FOR CREATING ADDON IN WEBADIN
	public void header_patient_information(WebDriver driver, boolean sep, boolean confirmed, boolean preparation) throws InterruptedException{
		
		WebElement ps = driver.findElement(By.xpath("//input[@id='preparationFlag']"));
		String mainWindow = driver.getWindowHandle();
		System.out.println(mainWindow);
		if(preparation==true){
			JavascriptExecutor jse = (JavascriptExecutor)driver;
			jse.executeScript("arguments[0].value = 'jude_bordallo@yahoo.com';", driver.findElement(By.xpath("//input[@id='emailAdd']")));
	        
	        String reserve_type;
			try{
				reserve_type = driver.findElement(By.xpath("//input[@id='reservationType']")).getAttribute("value");
			}catch(Exception e){
				reserve_type = "";
			}
			
			ps.click();
	        if(reserve_type.equals("ONLINE")||reserve_type.equals("online")){
	        	driver.findElement(By.xpath("//div[@id='preparationPopUp']/div[2]/div/input[1]")).click();
				driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				close_popup_after_submit_form(driver);
				ps = driver.findElement(By.xpath("//input[@id='preparationFlag']"));
	        }else{
	        	driver.findElement(By.xpath("//input[@value='PROCEED']")).click();
				driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	        }
	        Thread.sleep(20000);
	        driver.switchTo().window(mainWindow);
	        String a = ps.getAttribute("value");
			if(a.equals("Y")){
				System.out.println("Preparation Sent CheckBox is ticked.");
			}else{ System.out.println("Preparation Sent CheckBox is NOT ticked.");}	
		}
		WebElement vip = driver.findElement(By.xpath("//input[@id='vip']"));
		
		if(sep==true){
			vip.click();
			String a = vip.getAttribute("value");
			if(a.equals("Y")){
				System.out.println("SEP CheckBox is ticked.");
			}else{ System.out.println("SEP CheckBox is NOT ticked.");}
		}
		WebElement confirm = driver.findElement(By.xpath("//input[@id='confirmFlag']"));
		if(confirmed==true){
			confirm.click();
			String a = confirm.getAttribute("value");
			if(a.equals("Y")){
				System.out.println("Confirmed CheckBox is ticked.");
			}else{ System.out.println("Confirmed CheckBox is NOT ticked.");}
		}
	}
	//METHOD FOR CREATING TRANSACTION WITH CURRENT DATE IN ADMIN WITH PIN
	public void create_single_inpatient_with_pin_in_current_date(WebDriver driver, String pin, String room) throws Exception{
		add_reservation(driver, "single");
		search_pin_on_admin_console(driver, pin, "", "single");
		m.refresh_window(driver);
		input_the_reservation_type_of_patient(driver, "phone");
		input_the_contact_number_of_patient(driver);
		input_the_package_of_patient(driver, "onlineoverweight");
		input_the_date_of_admission_of_patient(driver, "current"); //marked
		input_the_time_of_admission_of_patient(driver);
		select_unit_single(driver, "admin");
		input_the_room_type_of_patient(driver, room);
		select_doctor_single(driver, "single", "");
		input_the_booker_of_patient(driver);
		select_patient_care_officer(driver);
		input_the_responsible_for_patient(driver, "self");
		submit_form(driver);
		close_popup_after_save_new_patient(driver);
	}
	
	//------------------------------------------------------------------------------------------------------------
	//THIS PORTION CORRESPONDS TO GENERIC WEB ELEMENTS OF ADMIN
	
	//SUBMIT FORM IN UPDATING SINGLE/PAIR TRANSACTIONS
	public void submit_form(WebDriver driver){
		driver.findElement(By.xpath("//input[@class='btnPrimary']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//CLOSE SUCCESS POPUP MESSAGE AFTER SUBMIT
	public void close_popup_after_submit_form(WebDriver driver){
		System.out.println(
				driver.findElement(By.xpath("//p[@id='errorPrompt']")).getText()
			);
			driver.findElement(By.xpath("//button[@id='modelClose']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			System.out.println("Closed popup submit");
	}
	//CLOSE ERROR POPUP MESSAGE AFTER SUBMIT
	public void close_error_popup_after_submit_form(WebDriver driver){
		driver.findElement(By.xpath("//button[@id='modalClose2']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//CLICK BACK BUTTON AFTER SUCCESSFULLY UPDATED
	public void select_back_button(WebDriver driver){
		WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(
			    ExpectedConditions.elementToBeClickable(
			        By.xpath("//input[@value='BACK']")));
		driver.findElement(By.xpath("//input[@value='BACK']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//DISPLAY MESSAGE AFTER SUBMITTING TO HCS THE TRANSACTION
	public void display_message_to_hcs(WebDriver driver){
		WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(
			    ExpectedConditions.elementToBeClickable(
			        By.xpath("//button[@id='modalClose']")));
		String message="";
		try{
			message = driver.findElement(By.xpath("//p[@id='errorMessage')]")).getText();	
		}catch(Exception e){
			message = driver.findElement(By.xpath("//html/body/div[2]/div[2]/p")).getText();	
		}
		System.out.println(message);
		driver.findElement(By.xpath("//button[@id='modalClose']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//WINDOW MAIN FOCUS
	public String window_main_focus(WebDriver driver){
		String mainWindowHandle=driver.getWindowHandle();
		return mainWindowHandle;
	}
	//POPUP SEARCH FOCUS
	public void window_focus_next(WebDriver driver){
		String mainWindow = window_main_focus(driver);
		Set<String> s = driver.getWindowHandles();
		Iterator<String> ite = s.iterator();
        while(ite.hasNext())
        {
             String popupHandle=ite.next().toString();
             if(!popupHandle.contains(mainWindow))
             {
                   driver.switchTo().window(popupHandle);
             }
        }
	}
	//CLICK ADD RESERVATION BUTTON FOR SINGLE/PAIR TRANSACTIONS
	public void add_reservation(WebDriver driver, String option){
		driver.findElement(By.xpath("//button[contains(.,'ADD RESERVATION')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		if(option.equals("single")){
			driver.findElement(By.xpath("//input[@id='single']")).click();
		}else{
			driver.findElement(By.xpath("//input[@id='pair']")).click();
		}
		driver.findElement(By.xpath("//input[@id='btnSelectSubmit']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//NEW RESERVATION FOR ADMIN (SINGLE: INPATIENT or OUTPATIENT / PAIR TRANSACTIONS)
	public void new_patient_button(WebDriver driver){
		driver.findElement(By.xpath("//div[@id='newPatient']/input")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	//CLOSE SUCCESS POPUP MESSAGE AFTER SAVING NEW PATIENT IN ADMIN
	public void close_popup_after_save_new_patient(WebDriver driver){
		try{
			driver.findElement(By.xpath("//button[@id='modalClose']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}catch(Exception e){
			WebDriverWait wait = new WebDriverWait(driver,30);
			wait.until(
			    ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='modalClose']")));
			driver.findElement(By.xpath("//button[@id='modalClose']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		}
		
	}

















}
